A script's file name and its own function name must match exactly - what that buys the compiler, how a call to it resolves, and what happens when a name collides with something else.
A new script starts from the Code tab's project browser, the same place an object is created - see The Code tab. Its name follows the same rule an object's name does: a valid identifier (a letter or underscore, then letters, digits or underscores), not one of GBML's own reserved words, and not already taken - by an object either. Objects and scripts share one namespace, so a script cannot be named the same as an existing object, and an object cannot be named the same as an existing script.
There is no required prefix the way obj_ is a common convention for objects - a script is usually just named for what it does: heal, apply_damage, spawn_wave.
A script is one file, scripts/<name>.gbml, and inside it, one top-level function whose own name is exactly <name>:
// scripts/heal.gbml
function heal(target, amount) {
target.hp += amount;
}This is what lets every other file in the project call the script by a plain name (heal(self, 5);) with nothing to import or include - the file name alone is enough for GBML to find it. Get the two out of sync - a typo in the function's own name, or renaming one but not the other by hand outside the editor - and GBML reports it the moment something tries to call the script, not when the file itself is saved:
// scripts/heal.gbml, saved with a typo in the function's own name
function heel(target, amount) {
target.hp += amount;
}scripts/heal.gbml exists, but has no function named `heal` in it, so this call has
nothing to run.The fix is exactly what the message says: rename the function back to match the file, spelled exactly. Renaming a script from the Code tab's project browser keeps the two in sync for you - it rewrites the function's own declaration, not just the file - so this only comes up if a script file is edited or renamed by hand, outside the editor. What a script compiles to covers why this particular mistake is caught at the call site rather than the moment the file is saved.
A script is called the same way any function is - its name, parentheses, the arguments it needs:
// scripts/shove.gbml
function shove(amount) {
x += amount;
}
// anywhere else in the project - an event, a cutscene step, or another script
shove(5);A script does not need to be told which object to work on. It works on the object that called it - see the next section.
When an event calls a script, the script does not get an object of its own. It runs as the object that called it, just as if you had typed the script's lines straight into that event. So inside a script:
self is the object that called it, and x, y, speed, sprite, depth and alarm are that object's own.move_step(...), path_start(...), shroud_cutout(...) and the other move_ calls move that object.draw_ call draws for that object, so it goes on that object's layer and at that object's depth, exactly as if the object had drawn it in its own Draw event.// scripts/lean_it.gbml
function lean_it(text: string, xx: real, yy: real, amount: int) {
draw_text_3d(xx, yy, text, TEXT_LEAN_BACK, amount);
}
// obj_hud's Draw GUI event
lean_it("HELLO", 10, 100, 3);
lean_it("AGAIN", 10, 120, 2);You can call a script like that as many times as you like in one event, with the same words or different ones, and one script can call another: the second one runs as the same object.
Who the script runs as depends on where it is called from:
with (obj_enemy) { ... }: it runs as each enemy the with visits, one after the other, because inside a with, self is the enemy being visited.array_foreach or array_filter by name: it runs as the object whose code wrote the array_foreach or array_filter line.x there has nothing to show it on, but it still builds and runs.Two built-in things belong to the object that called the script too: event_call(user0) runs the caller's own User 0 event, and instance_destroy(self) destroys the caller. An effect you start with effect_start belongs to the caller as well, so it ends when the caller is destroyed.
A script's parameters are ordinary names that belong to the script. If you give a parameter the same name as one of the object's own variables, the parameter wins inside the script:
// scripts/aim.gbml
function aim(x: real, y: real) {
// here `x` is the number you passed in. It is not the object's x.
draw_text(x, y, "here");
// the object's own x is still there, as self.x
draw_text(self.x, self.y, "me");
}
// in the event, you choose what to pass in
aim(x, y); // the object's own position, if you want it
aim(x + 20, 40); // or anything elseThat is on purpose. Passing x in an event gives the script a number that happens to equal the object's x, but it does not have to: you can pass the object's y as the script's x if you want. Nothing about the name x is special. When you want the object's real x inside the script, write self.x.
self as a whole objectA script can use a variable that only some kinds of object have - the hp of obj_enemy, say - and self on its own, as a whole object: to keep in a variable, to give another script that wants a ref<obj_enemy>, to give camera_follow.
// scripts/hurt.gbml
function hurt(amount: int) {
hp -= amount;
camera_follow(self);
}
// obj_enemy's Step event, and obj_boss's
hurt(2);The compiler sees which object is calling hurt, so it builds the script once for each kind of object that calls it, as if its lines had been typed into that kind's own events. For obj_enemy, hp is obj_enemy's hp and self is an obj_enemy; for obj_boss, they are obj_boss's. If obj_enemy keeps its hp as a whole number and obj_boss as a decimal, each gets the one that is its own. A child of obj_enemy that has no hp of its own has its parent's, so it can call hurt too. A script that uses nothing like this is not copied: it stays one script, exactly as it was.
The script itself is never refused. What cannot work is a call from an object that does not have what the script uses, and that is said at the call, in words:
`obj_coin` calls `hurt()`, which uses `hp`, but `obj_coin` has no `hp`.Give obj_coin an hp (in its Create event), or do not call hurt from it. The same goes for a script that calls a script that uses hp: the message names the chain.
Two places have no single kind of object to build the script for, and a call there is refused in the same way: inside with (all), which visits every kind at once (use with (obj_enemy) instead), and in a room's own Create code, which belongs to no object (use the Instance Creation code of an object placed in the room, where self is that object). A name that no object has is still not defined: check the spelling, or give an object that variable first. A name you make for yourself inside a script has to be made with var (var total = 0;), or kept for every object to read in global.score.
other works the same way. Inside a with block in a script, other is the object that called the script: other.x and other.hp are fine, and so is other on its own.
A script may also take the object as a parameter with its kind written down (function heal(who: ref<obj_hero>) { ... }, called from the event as heal(self);). That is still the way to work on an object other than the caller, and Script parameters covers it. The call is checked: handing in an object of a different kind from the one asked for is refused at the call.
Calling heal(...) normally means the script - but not if something closer already claims that name. A local variable or a parameter with the exact same name as a real script resolves first, before GBML even looks for a script by that name:
var heal = 3; // an ordinary int local, which happens to share a name with scripts/heal.gbml
heal(self, 5); // ERROR: `heal` is an int, not something you can call - the LOCAL
// wins here, so the real script is not reachable by this name in this scopeThis is worth knowing before it happens by accident: naming a loop counter, a parameter, or any other local the same as one of the project's own scripts shadows the script for the rest of that scope, rather than being flagged as a naming clash up front.
A script calling its own name is ordinary, legal GBML - nothing about how a call resolves treats a script calling itself any differently from calling any other script or built-in function. The same is true of two scripts calling each other: every script in the project gets its own signature worked out in one pass before anything is actually checked, so it makes no difference which one happens to be written, or called, first.
// scripts/factorial.gbml
/// @arg {int} n
/// @returns {int}
function factorial(n) {
if (n <= 1) { return 1; }
return n * factorial(n - 1);
}Renaming or duplicating a script from the Code tab keeps this safe automatically: it re-parses the script's own code, retargets its top-level declaration to the new name, and retargets any call inside the script that recurses into itself to match - so a duplicated factorial really does call the new copy of itself, not keep calling the original it was copied from.
A script file does not have to hold a callable function - Scripts covers this in full. A file with nothing in it, or with only a shared enum/#macro/struct declaration, is legitimate and stays completely quiet; the missing-function error only ever fires at an actual call to a name that turns out to have nothing behind it.